library(here)
library(ggplot2)
library(dplyr)
library(tibble)
library(dotwhisker)
load(here("Analysis", "alt_explanations_analysis.rda"), verbose = TRUE)

models <- list(
  sr_perc, sr_perc_rank, sr_soc_cap, sr_soc_cap_rank, comm_resp,
  comm_resp_rank
)

coeftab <- data.frame()

for (i in 1:length(models)) {
  coeftab[i, "dv_term"] <- colnames(models[[i]]@frame)[1]
  coeftab[i, "estimate"] <- summary(models[[i]])$coefficients[2, 1]
  coeftab[i, "std.error"] <- summary(models[[i]])$coefficients[2, 2]
  coeftab[i, "statistic"] <- summary(models[[i]])$coefficients[2, 4]
  coeftab[i, "p.value"] <- summary(models[[i]])$coefficients[2, 5]
}

coeftab$model <- rep(c("Raw", "Ranked"), 3)

coeftab$term <- rep(c(
  "Perceived Visible\nMinority in Community", "Social Cohesion",
  "Collective\nEfficacy"
), each = 2)

coeftab <- as_tibble(coeftab)
coeftab2 <- coeftab %>% filter(model == "Raw" & term %in% c("Perceived Visible\nMinority in Community", "Social Cohesion", "Collective\nEfficacy"))

pdf(file = here("Figures_Tables", "alt_explanations_plot_new.pdf"), width = 8, height = 6, pointsize = 16)
dwplot(coeftab2,
  dot_args = list(size = 3, pch = 21, color = "black", fill = "black"),
  whisker_args = list(color = "black", size = 1),
  vline = geom_vline(
    xintercept = 0,
    colour = "grey60",
    linetype = 2
  )
) + # plot line at zero _behind_ coefs
  theme_classic(base_size = 18) +
  xlab("Coefficient Estimate (Outcome Scale 0-1)") + ylab("") +
  # ggtitle("Effects of Symbolic Racism") +
  theme(
    plot.title = element_text(face = "bold", hjust = 0.3),
    legend.position = "none", # c(0.05, 0.78),
    # legend.justification = c(0, 0),
    # legend.background = element_rect(colour = "grey80"),
    # legend.title = element_text(size = 14),
    ## axis.line = element_blank(),
    ## axis.line = element_line(colour = "black"),
    panel.grid.major = element_blank(),
    panel.grid.minor = element_blank(),
    panel.border = element_blank(),
    panel.background = element_blank()
    ## panel.border = element_rect(fill = NA)
  ) # +
# scale_colour_grey(
#  start = .3,
#  end = .7,
#  name = "Model"
# )
dev.off()
